####
#
# Copyright 2007 B. Chandler Carruth
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
####
#
# ./lib/rcons/files/c_cxx/cxx_source_file_target.rb
#
# This file provides the basic C++ source file target functionality.
#
####

require( 'rcons/files/source_file_target' )
require( 'rcons/files/c_cxx/cpp_file_module' )
require( 'rcons/tools/cxx_compiler_target' )

module RCons

  # Provides functionality to manage C++ source file targets.  This class
  # provides intelligent defaults with respect to the name and the compiler
  # used based on initialization parameters and registered compilers.
  # A suitable compiler will be searched for and used if found.
  #
  # Extends +SourceFileTarget+.
  class CxxSourceFileTarget < SourceFileTarget
    # bring in common C/C++ Pre-Processing routiens
    include CppFileModule

    # Initializes a C++ source file target.  Determines which compiler to use
    # to compile the file based on registered compilers.  A lot of the
    # work is done by the parent class.
    def initialize( build_env, src, opts = Hash.new() )
      # early check for C++ compiler
      if opts[ :compiler ]
        compiler = opts[ :compiler ]
      else
        compiler = CxxCompilerTarget.best_klass().new( build_env )
      end

      raise( ArgumentError,
        "Unexpected type for compiler argument.",
        caller() ) unless compiler.kind_of?( CxxCompilerTarget )

      if opts[ :name ]
        name = opts[ :name ].to_s()
      else
        if src.to_s() =~ /\.[^\.]*$/
          name = "#{$`}.o"
        else
          name = "#{src}.o"
        end
      end

      # handle all the defaults for a source file
      super( build_env, src, name, compiler, opts )
    end

    # Generates pre-dependencies specific to C++ source files using the
    # CppFileModule, then proceeds with the build.
    def build_pre_deps( changed )
      # Hand off to CppFileModule to generate the deps...
      generate_deps()

      # And now dump back to the predefined processing.
      super
    end
  end
end

